package com.enhe.gck.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.enhe.gck.exception.BusinessException;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 锁 工具类
 */
public class LockUtil {

    private static final Lock lock = new ReentrantLock();

    // 声明一个全局可用的线程安全集合
    private volatile static Map<String, String> globalMap = new ConcurrentHashMap<>();

    public static final String DEFAULT_VALUE = "1";

    /**
     * 判断是否有key
     */
    public static boolean containsKey(String key) {
        if (Objects.isNull(key)) {
            throw new BusinessException("key不能为空");
        }

        if (lock.tryLock()) {
            try {
                return globalMap.containsKey(key);
            } finally {
                lock.unlock();
            }
        }

        return false;
    }

    /**
     * 添加key
     *
     * @param key   key
     * @param value value
     */
    public static void setKey(String key, String value) {
        if (Objects.isNull(key)) {
            throw new BusinessException("key不能为空");
        }

        if (lock.tryLock()) {
            try {
                globalMap.put(key, value);
            } finally {
                lock.unlock();
            }
        }
    }

    public static void setKey(String key) {
        setKey(key, DEFAULT_VALUE);
    }

    public static void setKeyBatch(List<String> keyList) {
        if (lock.tryLock()) {
            try {
                keyList.forEach(key -> {
                    globalMap.put(key, DEFAULT_VALUE);
                });
            } finally {
                lock.unlock();
            }
        }
    }

    /**
     * 获取key的value
     *
     * @param key key
     * @return Optional<Object>
     */
    public static Optional<Object> getValue(String key) {
        if (Objects.isNull(key)) {
            return Optional.empty();
        }

        if (lock.tryLock()) {
            try {
                if (globalMap.containsKey(key)) {
                    return Optional.of(globalMap.get(key));
                } else {
                    return Optional.empty();
                }
            } finally {
                lock.unlock();
            }
        }

        return Optional.empty();
    }

    /**
     * 删除key
     *
     * @param key key
     */
    public static void remove(String key) {
        if (Objects.isNull(key)) {
            return;
        }

        if (!containsKey(key)) {
            return;
        }

        if (lock.tryLock()) {
            try {
                globalMap.remove(key);
            } finally {
                lock.unlock();
            }
        }
    }

    public static void removeKeyBatch(List<String> batchLockList) {
        if (CollectionUtil.isEmpty(batchLockList)) {
            return;
        }
        if (lock.tryLock()) {
            try {
                batchLockList.forEach(key -> {
                    globalMap.remove(key);
                });
            } finally {
                lock.unlock();
            }
        }
    }
}
